﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using COM.Rights.Model;
using COM.Rights.BLL;
using System.Data;
using System.Text;
using COM.PreAlarm.Model;
using COM.PreAlarm.BLL;
using System.Web.UI.WebControls;


public partial class Pages_OnLineRateTotal : AnalyticsListBase
{  
	#region 重写方法

    
     protected string _FusionCartXML = "";

    protected override void OnLoad()
    {

        if (!IsPostBack)
        {
            fromData.Text = DateTime.Now.ToString("yyyy-MM-dd");
            toData.Text = DateTime.Now.ToString("yyyy-MM-dd");


            //初始化市
            MdlistInfo _MdlistInfo = new MdlistInfo();
            _MdlistInfo.TranType = "GetCityByUser";
            _MdlistInfo.UID = ZASession.Account;

            DataTable dt = Mdlist.GetDataTableByModel(_MdlistInfo);
            if (dt != null)
            {
                if (dt.Rows.Count == 1)
                {
                    //一条记录默认选中
                    ZABinder.FillList(StringHelper.FormatObjToString(dt.Rows[0]["Name"]), ddlCity, false, dt);
                    //填充区
                    _MdlistInfo.TranType = "GetDistrictByUser";
                    _MdlistInfo.UID = ZASession.Account;
                    _MdlistInfo.Type = ddlCity.SelectedValue;
                    DataTable dtDistrict = Mdlist.GetDataTableByModel(_MdlistInfo);
                    //一条记录默认选中
                    if (dtDistrict != null && dtDistrict.Rows.Count == 1)
                    {
                        ZABinder.FillList(StringHelper.FormatObjToString(dtDistrict.Rows[0]["name"]), ddlDistrict, false, dtDistrict);
                    }
                    else {
                        ZABinder.FillList(string.Empty, ddlDistrict, true, dtDistrict);
                    }

                }
                else
                {
                    ZABinder.FillList(string.Empty, ddlCity, true, dt);
                    ddlDistrict.Items.Add(new ListItem("--请选择--", ""));
                }

            }
            else {
                //没有市的权限，显示所管控的所有区
                ZABinder.FillList(string.Empty, ddlCity, true, dt);
                ddlDistrict.Items.Add(new ListItem("--请选择--", ""));
            }
        }
           
        

      

        base.OnLoad();
    }

    public static string ChartRoot(string caption, string subcaption)
    {
        //return "<chart palette=\"2\" caption=\"" + caption + "\" shownames=\"1\" showvalues=\"0\" decimals=\"3\" numberPrefix=\"\">";
        //return "<chart caption=\"" + caption + "\" shownames=\"1\"  baseFontSize=\"16\" showvalues=\"0\" decimals=\"0\" numberPrefix=\"\">";
        return string.Format("<chart caption=\"{0}\" subcaption = \"{1}\" shownames=\"1\"  baseFontSize=\"12\" showvalues=\"0\" decimals=\"0\" numberPrefix=\"\">", caption, subcaption);

    }

    protected string CreatXML(DataTable dt)
    {
        //string[] tName = { "点击数", "独立IP数", "对话数", "成单数" };

        StringBuilder categories = new StringBuilder();
        categories.Append(ChartRoot("设备状态统计图", ""));
        categories.Append("<categories>");
        foreach (DataRow dr in dt.Rows) {
            categories.Append("<category   label=\"");
            categories.Append(StringHelper.FormatObjToString(dr["City"]));
            categories.Append("\" />");
        }
        categories.Append("</categories>");
        categories.Append(InitDateSet(dt));
        categories.Append("  </chart>");
        
       return Server.HtmlEncode(categories.ToString());
    }

    public string InitDateSet(DataTable dt)
    {
        string[] colour = { "1D8BD1", "F1683C", "F6BD0F", "8BBA00", "AFD8F8", "1D8BD1", "F1683C", "F6BD0F", "8BBA00", "AFD8F8" };
        StringBuilder dataSet = new StringBuilder();
        //dataSet.Append("<dataset seriesName=\"在线率\"  color=\"");
        //string dataSet = "";
            
     
            dataSet.Append("<dataset seriesName=\"在线率(%25)\"   color=\"1D8BD1\" showValues=\"0\">");
            foreach (DataRow dr in dt.Rows)
            {
                dataSet.Append("<set value=\"" );
                dataSet.Append(StringHelper.FormatObjToString(dr["OnLineRate"]));
                dataSet.Append( "\" />");
            }
            dataSet.Append("</dataset>");
            dataSet.Append( "<dataset seriesName=\"在线设备数(台)\"  color=\"F1683C\" showValues=\"0\">");
            foreach (DataRow dr in dt.Rows)
            {
                dataSet.Append("<set value=\"" );
                dataSet.Append(StringHelper.FormatObjToString(dr["OnLineCount"]));
                dataSet.Append( "\" />");
              
            }
            dataSet.Append("</dataset>");
            
            dataSet.Append( "<dataset seriesName=\"总设备数(台)\"  color=\"8BBA00\" showValues=\"0\">");
            foreach (DataRow dr in dt.Rows)
            {
                dataSet.Append("<set value=\"" );
                dataSet.Append(StringHelper.FormatObjToString(dr["Total"]));
                dataSet.Append( "\" />");
              
            }
            dataSet.Append("</dataset>");

           
       
        return dataSet.ToString();
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (ddlCity.SelectedValue != string.Empty)
        {
            //初始化区
            MdlistInfo _MdlistInfo = new MdlistInfo();
            _MdlistInfo.TranType = "GetDistrictByUser";
            _MdlistInfo.UID = ZASession.Account;
            _MdlistInfo.Type = ddlCity.SelectedValue;
            ZABinder.FillList(string.Empty, ddlDistrict, true, Mdlist.GetDataTableByModel(_MdlistInfo));
        }

    }

    protected override void OnGridDataBind()
    {
        BdOrder _BdOrder = new BdOrder();
        List<DBParameter> _listDBParameter = new List<DBParameter>();
        _listDBParameter.Add(new DBParameter("@TranType", "OnLineRateTotal"));
        _listDBParameter.Add(new DBParameter("@OrderDateBegin", fromData.Text));
        _listDBParameter.Add(new DBParameter("@OrderDateEnd", toData.Text));
        _listDBParameter.Add(new DBParameter("@UID", ZASession.Account));
        _listDBParameter.Add(new DBParameter("@City", ddlCity.SelectedValue));
        _listDBParameter.Add(new DBParameter("@District", ddlDistrict.SelectedValue));
        _listDBParameter.Add(new DBParameter("@Equip_NO", Equip_No.Text));
        IDataReader sdr = _BdOrder.ExecuteReader("[dbo].[report]", _listDBParameter, System.Data.CommandType.StoredProcedure);

        DataTable dt = new DataTable();
        dt.Load(sdr);
        dt.Columns.Add("OnLineRatePrecent");
        foreach (DataRow dr in dt.Rows) {
            dr["OnLineRatePrecent"] = StringHelper.FormatObjToString(dr["OnLineRate"]) + "%";
        }

         BaseMainPager.RecordCount = dt.Rows.Count;
         gvInfos.DataSource = ZPager.TablePage(dt, BaseMainPager.PageSize, BaseMainPager.CurrentPageIndex);
         gvInfos.DataBind();

         _FusionCartXML = CreatXML(dt);
    }


	#endregion 	重写方法
}